package com.woniu.tms.manage.controller;

import com.woniu.tms.entity.RolePermission;
import com.woniu.tms.manage.service.RolePermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("manage/rolepermission")
public class RolePeremissionController {

    @Autowired
    RolePermissionService rolePermissionService;

    /**
     * 增加/修改角色权限
     *
     * @param rid
     * @param pids
     * @return
     */
    @RequestMapping("assignDo")
    @ResponseBody
    public Map<String, Object> assignDo(Integer rid, String pids) {
        Map<String, Object> map = new HashMap<>();
        try {
            rolePermissionService.assign(rid, pids);
            map.put("result", true);
        } catch (Exception e) {
            map.put("result", false);
        }
        return map;
    }

    /**
     * 展示权限树
     *
     * @param rid
     * @return
     */
    @RequestMapping("assignList")
    @ResponseBody
    public Map<String, Object> assignList(Integer rid) {
        Map<String, Object> map = new HashMap<>();
        try {
            StringBuilder str = new StringBuilder();
            List<RolePermission> rolePermissionList = rolePermissionService.getByRid(rid);
            if (rolePermissionList.size() != 0) {
                for (RolePermission rolePermission : rolePermissionList) {
                    str.append(rolePermission.getPid()).append(",");
                }
                String pids = str.substring(0, str.length() - 1);
                map.put("pids", pids);
            }
            map.put("result", true);
        } catch (Exception e) {
            map.put("result", false);
        }
        return map;
    }

}
